﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.Core.Service;

namespace Wolfy.NetDisk.Service
{
    public class ImageManagerService : IImageManagerService
    {
        /// <summary>
        /// 等比例压缩图片
        /// </summary>
        /// <param name="savePath"></param>
        /// <param name="stream"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void CompressImage(string savePath, System.IO.Stream stream, int width, int height)
        {
            //从输入流中获取上传的image对象
            using (Image img = Image.FromStream(stream))
            {
                //根据压缩比例求出图片的宽度
                int intWidth = width / height * img.Height;
                int intHeight = img.Width * height / width;
                //画布
                using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(img, new Size(width, height)))
                {
                    //在画布上创建画笔对象
                    using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
                    {
                        //将图片使用压缩后的宽高,从0，0位置画在画布上
                        graphics.DrawImage(img, 0, 0, width, height);
                        //保存图片
                        bitmap.Save(savePath);
                    }
                }
            }
        }
        /// <summary>
        /// 创建验证码
        /// </summary>
        /// <param name="len">验证码长度</param>
        /// <param name="strCode">验证码</param>
        /// <returns></returns>

        public byte[] CreateVerifyCode(int len, out string strCode)
        {
            MemoryStream stream = new MemoryStream();
            byte[] buffer = null;
            //噪线 噪点
            Color[] colors = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
            //验证码字体
            string[] fonts = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
            //验证码内容
            char[] charactars = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'S', 'Y', 'Z' };
            //生成验证码字符串
            StringBuilder sb = new StringBuilder();
            Random r = new Random();
            for (int i = 0; i < len; i++)
            {
                char codeChar = charactars[r.Next(0, charactars.Length)];
                sb.Append(codeChar);
            }
            strCode = sb.ToString();
            using (Bitmap bitmap = new Bitmap(len * 25, 35))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.Clear(Color.White);
                    for (int i = 0; i < 10; i++)
                    {
                        int x1 = r.Next(100);
                        int y1 = r.Next(40);
                        int x2 = r.Next(100);
                        int y2 = r.Next(40);
                        Color color = colors[r.Next(colors.Length)];
                        g.DrawLine(new Pen(color), x1, y1, x2, y2);
                    }
                    //将字符串画入图片
                    for (int i = 0; i < strCode.Length; i++)
                    {
                        string font = fonts[r.Next(fonts.Length)];
                        Font fnt = new Font(font, 18);
                        Color color = colors[r.Next(colors.Length)];
                        g.DrawString(strCode[i].ToString(), fnt, new SolidBrush(color), (float)i * 20 + 8, (float)8);
                    }
                    //画噪点
                    for (int i = 0; i < 100; i++)
                    {
                        int x = r.Next(bitmap.Width);
                        int y = r.Next(bitmap.Height);
                        Color color = colors[r.Next(colors.Length)];
                        bitmap.SetPixel(x, y, color);
                    }
                }
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                buffer = stream.ToArray();
            }

            return buffer;
        }
    }
}
